package com.itheima.controller;

import com.itheima.domain.Role;
import com.itheima.domain.User;
import com.itheima.service.RoleSeveice;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RequestMapping("/user")
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleSeveice roleSeveice;

    @RequestMapping("/saveUI")
    public ModelAndView saveUI(){
        List<Role> rolelist = roleSeveice.list();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("rolelist",rolelist);
        modelAndView.setViewName("user-add");
        return modelAndView;
    }

    @RequestMapping("/save")
    public String save(User user,Long[] roleIds){
        userService.save(user,roleIds);
        return "redirect:/user/list";
    }

    @RequestMapping("/del/{userId}")
    public String delUser(@PathVariable("userId") Long userId){
        userService.delUser(userId);
        return "redirect:/user/list";
    }


    @RequestMapping("/list")
    public ModelAndView list(){
       List<User> userlist = userService.list();
       ModelAndView modelAndView = new ModelAndView();
       modelAndView.addObject("userlist",userlist);
       modelAndView.setViewName("user-list");
       return modelAndView;
    }

}
